package com.zax.servlet;

import com.zax.model.User;
import com.zax.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user_changepwd")
public class UserChangePwdServlet extends HttpServlet {
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//    }


    private UserService uSersvice = new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String password = request.getParameter("password");
        String newPwd = request.getParameter("newPassword");

        User u = (User)request.getSession().getAttribute("user");
        if (password.equals(u.getPassword())){
            u.setPassword(newPwd);
            uSersvice.updatePwd(u);
            request.setAttribute("msg" , "密码修改成功！");
            request.getRequestDispatcher("/user_center.jsp").forward(request,response);
        }else {
            request.setAttribute("failMsg" , "密码修改失败，原密码不正确，请检查后重新输入！");
            request.getRequestDispatcher("/user_center.jsp").forward(request,response);
        }
    }
}
